package com.tongji.admin.service
{
	import com.tongji.admin.event.BrandAdminEvent;
	import com.tongji.admin.event.CantavilAdminEvent;
	import com.tongji.admin.event.GoodsTypeAdminEvent;
	import com.tongji.admin.event.TypeAdminEvent;
	import com.tongji.admin.vo.Brand;
	import com.tongji.admin.vo.Cantavil;
	import com.tongji.admin.vo.Goods;
	import com.tongji.admin.vo.Type;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/7/
	 */ 
	public class HouseDiyAdminService extends Actor
	{
		public var remoteObject:RemoteObject;
		
		public function HouseDiyAdminService()
		{
			super();
			
			var amfChannel:Array = [new AMFChannel(null, "http://localhost:88/Gateway.aspx")];
			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			
			remoteObject = new RemoteObject("fluorine");
			remoteObject.source = "HouseDiyService.HouseDiyAdmin";
			remoteObject.showBusyCursor = true;
			remoteObject.channelSet = channelSet;
			
			remoteObject.getOperation("addABrand").addEventListener(ResultEvent.RESULT, addBrandHandler);
			remoteObject.getOperation("addABrand").addEventListener(FaultEvent.FAULT, faultHandler);
			
			remoteObject.getOperation("addACantavil").addEventListener(ResultEvent.RESULT, addCantavilHandler);
			remoteObject.getOperation("addACantavil").addEventListener(FaultEvent.FAULT, faultHandler);
			
			remoteObject.getOperation("addAGoods").addEventListener(ResultEvent.RESULT, addGoodsHandler);
			remoteObject.getOperation("addAGoods").addEventListener(FaultEvent.FAULT, faultHandler);
			
			remoteObject.getOperation("getGoodsType").addEventListener(ResultEvent.RESULT, getGoodsTypeHandler);
			remoteObject.getOperation("getGoodsType").addEventListener(FaultEvent.FAULT, faultHandler);
			
			remoteObject.getOperation("addType").addEventListener(ResultEvent.RESULT, addTypeHandler);
			remoteObject.getOperation("addType").addEventListener(FaultEvent.FAULT, faultHandler);
			
			remoteObject.getOperation("getType").addEventListener(ResultEvent.RESULT, getTypeHandler);
			remoteObject.getOperation("getType").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		
		public function addBrand(brand:Brand):void
		{
			remoteObject.addABrand(brand);
		}
		
		public function addGoods(goods:Goods):void
		{
			remoteObject.addAGoods(goods);
		}
		
		public function addCantavil(cantavil:Cantavil):void
		{
			remoteObject.addACantavil(cantavil);
		}
		
		public function getGoodsType():void
		{
			remoteObject.getGoodsType();
		}
		
		public function addAType(type:Type):void
		{
			remoteObject.addType(type);
		}
		
		public function getAllType():void
		{
			remoteObject.getType();
		}
		
		private function getGoodsTypeHandler(event:ResultEvent):void
		{
			dispatch(new GoodsTypeAdminEvent(GoodsTypeAdminEvent.UPDATE_TYPES, null, event.result as ArrayCollection));
		}
		
		private function addBrandHandler(event:ResultEvent):void
		{
			dispatch(new BrandAdminEvent(BrandAdminEvent.GET_ALL_BRANDS, null, event.result as ArrayCollection));
		}
		
		private function addCantavilHandler(event:ResultEvent):void
		{
			dispatch(new CantavilAdminEvent(CantavilAdminEvent.GET_ALL_CANTAVIL, null, event.result as ArrayCollection));
		}
		
		private function addGoodsHandler(event:ResultEvent):void
		{
			
		}
		
		private function addTypeHandler(event:ResultEvent):void
		{
			
		}
		
		
		private function getTypeHandler(event:ResultEvent):void
		{
			dispatch(new TypeAdminEvent(TypeAdminEvent.UPDATE_TYPEARRAY, null, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}